<template>
  <div id="picture-manage-page">
    <!--    搜索-->
    <a-form layout="inline">
      <a-form-item label="空间名称">
        <a-input
          v-model:value="params.spaceName"
          @change="doSearch"
          placeholder="请输入空间名称"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="用户id">
        <a-input
          v-model:value="params.userId"
          @change="doSearch"
          placeholder="根据用户id查询"
          allow-clear
        />
      </a-form-item>

      <a-form-item label="空间等级">
        <a-select
          v-model:value="params.spaceLevel"
          @change="doSearch"
          placeholder="根据空间等级查询"
          allow-clear
          style="width: 200px"
          :options="SPACE_LEVEL_OPTIONS"
        >
        </a-select>
      </a-form-item>

      <a-form-item label="空间类型">
        <a-select
          v-model:value="params.spaceType"
          @change="doSearch"
          placeholder="根据空间类型查询"
          allow-clear
          style="width: 200px"
          :options="SPACE_TYPE_OPTIONS"
        >
        </a-select>
      </a-form-item>

      <a-form-item>
        <a-space>
          <a-button type="primary" @click="doSearch">查询</a-button>
          <a-button type="primary" @click="doReset">重置</a-button>
        </a-space>
      </a-form-item>
    </a-form>

    <a-flex justify="space-between" gap="middle" style="margin: 10px">
      <a-space><a-button type="primary" href="/space/add">创建空间</a-button></a-space>
      <a-space>
        <a-button href="/space/analysis?queryPublic=1">分析公共图库</a-button>
        <a-button href="/space/analysis?queryAll=1">分析全部图库</a-button>
      </a-space>
    </a-flex>

    <!--    表格-->
    <a-table
      :columns="columns"
      :data-source="pictureList"
      :pagination="pagination"
      @change="doTableChange"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'id'">
          <a>
            {{ record.id }}
          </a>
        </template>

        <template v-else-if="column.dataIndex === 'createTime' || column.dataIndex === 'editTime'">
          {{ dayjs(record.createTime).format('YYYY-MM-DD HH:mm:ss') }}
        </template>

        <template v-else-if="column.dataIndex === 'spaceUseInfo'">
          <a-descriptions :column="1">
            <a-descriptions-item label="数量">{{ record.maxCount }} 张</a-descriptions-item>
            <a-descriptions-item label="大小">{{ formatSize(record.maxSize) }}</a-descriptions-item>
          </a-descriptions>
        </template>

        <template v-else-if="column.dataIndex === 'spaceLevel'">
          <a-tag :color="record.spaceLevel === 2 ? 'black' : 'pink' ">{{ SPACE_LEVEL_OPTIONS[record.spaceLevel].label }}</a-tag>
        </template>

        <template v-else-if="column.dataIndex === 'spaceType'">
          <a-tag :color="record.spaceType === 0 ? 'red' : 'blue' ">{{ SPACE_TYPE_OPTIONS[record.spaceType].label }}</a-tag>
        </template>

        <template v-else-if="column.key === 'action'">
          <span>
            <a-button type="link" :href="`/space/add?id=${record.id}`">编辑</a-button>

            <a-popconfirm title="你确定要删除吗？" @confirm="doDeleteSpace(record.id)">
              <a-button danger>删除</a-button>
            </a-popconfirm>
          </span>
        </template>
      </template>
    </a-table>
  </div>
</template>
<script lang="ts" setup>
import { computed, onMounted, reactive, ref } from 'vue'
import dayjs from 'dayjs'
import { formatSize } from '@/utils'
import { deleteSpaceUsingPost, listSpaceByPageUsingGet } from '@/api/spaceController.ts'
import { SPACE_LEVEL_OPTIONS } from '@/constants/level.ts'
import { SPACE_TYPE_OPTIONS} from '@/constants/space.ts'

const columns = [
  {
    title: 'id',
    dataIndex: 'id',
    width: 80,
    ellipsis: true,
  },
  {
    title: '空间名称',
    dataIndex: 'spaceName',
    width: 200,
    ellipsis: true,
  },
  {
    title: '空间级别',
    dataIndex: 'spaceLevel',
    width: 200,
  },
  {
    title: '空间类型',
    dataIndex: 'spaceType',
    width: 200,
  },
  {
    title: '使用情况',
    dataIndex: 'spaceUseInfo',
    width: 200,
  },
  {
    title: '用户 id',
    dataIndex: 'userId',
    width: 80,
    ellipsis: true,
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    width: 200,
  },
  {
    title: '编辑时间',
    dataIndex: 'editTime',
    width: 200,
  },
  {
    title: '操作',
    key: 'action',
  },
]

// 请求参数
const params = reactive<API.listSpaceByPageUsingGETParams>({
  current: 1,
  pageSize: 10,
  sortField: 'update_time',
  sortOrder: 'descend',
})

//
//  total
const total = ref(0)
// 获取数据
const pictureList = ref<API.Space[]>([])

const getSpaceList = async () => {
  const res = await listSpaceByPageUsingGet({ ...params })
  if (res.data.code !== 0) {
    return
  }
  pictureList.value = res.data.data.records
  total.value = res.data.data.total
}

// 分页数据
const pagination = computed(() => {
  return {
    current: params.current,
    showQuickJumper: true,
    pageSize: params.pageSize,
    total: total.value,
    showSizeChanger: true,
    pageSizeOptions: ['5', '10', '20'],
    showTotal: (total) => `共 ${total} 条`,
  }
})

// 分页改变
const doTableChange = (page) => {
  params.current = page.current
  params.pageSize = page.pageSize
  getSpaceList()
}

// 查询
const doSearch = () => {
  params.current = 1
  getSpaceList()
  console.log(params)
}

// 重置
const doReset = () => {
  params.current = 1
  params.pageSize = 10
  params.spaceName = undefined
  params.spaceType = undefined
  params.spaceLevel = undefined
  getSpaceList()
}

// 删除空间
const doDeleteSpace = async (id: string) => {
  const res = await deleteSpaceUsingPost({ id: id })
  if (res.data.code !== 0) {
    return
  }
  await getSpaceList()
}

// 页面渲染完成时，获取数据
onMounted(() => {
  getSpaceList()
})
</script>
